package com.wanshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanshi.client.ProductsClient;
import com.wanshi.entity.Orders;
import com.wanshi.entity.Products;
import com.wanshi.entity.User;
import com.wanshi.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-21
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private ProductsClient ProductsClient;

    //1 根据id查询订单
    @GetMapping("/{id}")
    public Orders findById(@PathVariable String id){
        Orders order = ordersService.getById(id);

        List<Products> list = ProductsClient.findProdsAndNumByOrderId(id);
        order.setProds(list);
        return order;
    }

    //2 查询所有订单
    @GetMapping("/list")
    public List<Orders> list(){
        return ordersService.list();
    }

    //3 新增订单
    @PostMapping
    public String addOrder(@RequestBody Orders order){
        ordersService.save(order);
        return "添加订单成功";
    }

    //根据用户id查询订单
    @GetMapping("/user/{userid}")
    public List<Orders> findByUserId(@PathVariable String userid){
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userid);
        return ordersService.list(wrapper);
    }

}

